package com.quanshu.exchange.ui.gui.profile.order_fragment;

import android.graphics.Paint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baselibrary.util.glide.GlideUtils;
import com.gyf.barlibrary.ImmersionBar;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.api.ShopInterface;
import com.quanshu.exchange.support.config.Config;
import com.quanshu.exchange.support.entity.OrderDetailsEntity;
import com.quanshu.exchange.support.net.BaseResponse;
import com.quanshu.exchange.support.net.core.BaseObserver;
import com.quanshu.exchange.support.net.core.BaseTransformer;
import com.quanshu.exchange.support.net.core.NetClient;
import com.quanshu.exchange.support.utils.AppUtils;
import com.quanshu.exchange.support.utils.LogUtil;
import com.quanshu.exchange.ui.base.BaseActivity;
import com.quanshu.shop.base.view.BaseTitleBar;

import butterknife.BindView;
import butterknife.ButterKnife;

public class RefundDetailActivity extends BaseActivity {


    @BindView(R.id.order_details_image)
    ImageView orderDetailsImage;
    @BindView(R.id.pay_flag)
    ImageView payFlag;
    @BindView(R.id.order_details_price_pianyi)
    TextView orderDetailsPricePianyi;
    @BindView(R.id.order_details_price_gui)
    TextView orderDetailsPriceGui;
    @BindView(R.id.order_details_number)
    TextView orderDetailsNumber;
    @BindView(R.id.order_details_shop_name)
    TextView orderDetailsShopName;
    @BindView(R.id.order_details_serial_number)
    TextView orderDetailsSerialNumber;
    @BindView(R.id.tv_refund_money)
    TextView tvRefundMoney;
    @BindView(R.id.tv_refund_apply_time)
    TextView tvRefundApplyTime;
    @BindView(R.id.tv_refund_reason)
    TextView tvRefundReason;
    @BindView(R.id.title)
    BaseTitleBar title;
    @BindView(R.id.tv_refund_tip1)
    TextView tvRefundTip1;
    @BindView(R.id.tv_rest_time)
    TextView tvRestTime;
    @BindView(R.id.order_details_price_llt)
    LinearLayout orderDetailsPriceLlt;

    private String orderId;
    private OrderDetailsEntity orderDetailsEntity;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_refund_detail);
        ButterKnife.bind(this);
        ImmersionBar.with(this).titleBar(title).statusBarColor(R.color.white).statusBarDarkFont(true).init();
        initIntent();
        initData();
        initListener();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }

    private void initIntent() {
        orderId = getIntent().getStringExtra("orderId");
    }

    private void initView() {

        if (orderDetailsEntity != null) {
            GlideUtils.setRoundImageView(this, Config.URL_HOST_SHOP + orderDetailsEntity.getImg(), R.drawable.default_img, orderDetailsImage);
            if (orderDetailsEntity.getPayment_options() == 1) {
                payFlag.setImageResource(R.drawable.icon_njf_2_0);
            } else {
                payFlag.setImageResource(R.drawable.icon_rmb_2_0);
            }

            orderDetailsPricePianyi.setText(orderDetailsEntity.getGoods_price());

            orderDetailsPriceGui.setText(orderDetailsEntity.getReal_price());
            orderDetailsPriceGui.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            orderDetailsNumber.setText("x" + orderDetailsEntity.getGoods_nums());
            orderDetailsShopName.setText(orderDetailsEntity.getGoods_name());

            tvRefundMoney.setText("退款金额：" + orderDetailsEntity.getRefundment_price());
            tvRefundApplyTime.setText("申请时间：" + orderDetailsEntity.getRefundment_time());
            tvRefundReason.setText("退款原因：" + orderDetailsEntity.getRefundment_reason());
        }
    }

    private void initData() {
        NetClient.getInstance().getShopService(ShopInterface.class)
                .getOrderDetail(AppUtils.getSignParams(orderId), orderId)
                .compose(new BaseTransformer<>(this))
                .subscribe(new BaseObserver<BaseResponse<OrderDetailsEntity>>() {
                    @Override
                    public void onSuccess(BaseResponse<OrderDetailsEntity> response) {
                        if (response.getData() != null) {
                            orderDetailsEntity = response.getData();
                            initView();
                        }
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        LogUtil.printException(e);
                    }
                });

    }

    private void initListener() {
        title.setCenterText("退款原因").setLeftImage(R.drawable.icon_back_black_2_0).setOnClickBaseTitleListener(new BaseTitleBar.OnClickBaseTitleBarListener() {
            @Override
            public void onclickLeft() {
                finish();
            }

            @Override
            public void onclickRight() {

            }
        });
    }


}
